package com.quanyan.user.service.impl;

import com.quanyan.user.mapper.TbFeedbackImageMapper;
import com.quanyan.user.mapper.TbFeedbackMapper;
import com.quanyan.user.model.db.TbFeedback;
import com.quanyan.user.model.db.TbFeedbackImage;
import com.quanyan.user.model.vo.request.ReqFeedback;
import com.quanyan.user.service.FeedbackService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by huadongyang on 2016/11/7.
 */
@Service
public class FeedbackServiceImpl implements FeedbackService {
    private static final Logger logger = LoggerFactory.getLogger(FeedbackServiceImpl.class);

    @Autowired
    private TbFeedbackMapper tbFeedbackMapper;
    @Autowired
    private TbFeedbackImageMapper tbFeedbackImageMapper;

    @Override
    public void saveFeedback(ReqFeedback reqFeedback) {
        if (reqFeedback != null) {
            TbFeedback tbFeedback = new TbFeedback();
            tbFeedback.setMobile(reqFeedback.getMobile());
            tbFeedback.setContent(reqFeedback.getContent());
            tbFeedback.setUid(reqFeedback.getUid());
            if (reqFeedback != null ){
                tbFeedback.setAppType(reqFeedback.getAppType());
            }
            int result = 0;
            try {
                result = tbFeedbackMapper.insertSelective(tbFeedback);
                List<String> imageUrls = reqFeedback.getImageUrls();
                if (result == 1 && !imageUrls.isEmpty() && imageUrls.size() > 0) {
                    TbFeedbackImage tbFeedbackImage = new TbFeedbackImage();
                    tbFeedbackImage.setFeedbackId(tbFeedback.getId());
                    for (String url : imageUrls) {
                        tbFeedbackImage.setImgurl(url);
                        tbFeedbackImageMapper.insertSelective(tbFeedbackImage);
                    }
                }
            } catch (Exception e) {
                logger.error("保存用户反馈信息失败！:{}", e.getMessage());
            }

        }
    }
}
